<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>重置密码 - 医院药品管理系统</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <style>
        html, body {width: 100%; height: 100%; overflow: hidden}
        body {background: #1E9FFF;}
        body:after {content:''; background-repeat:no-repeat; background-size:cover; filter:blur(3px); position:absolute; top:0; left:0; right:0; bottom:0; z-index:-1;}
        .layui-container {width: 100%; height: 100%; overflow: hidden}
        .admin-login-background {width:360px; height:500px; position:absolute; left:50%; top:50%; margin-left:-180px; margin-top:-250px;}
        .logo-title {text-align:center; letter-spacing:2px; padding:14px 0;}
        .logo-title h1 {color:#1E9FFF; font-size:25px; font-weight:bold;}
        .login-form {background-color:#fff; border:1px solid #fff; border-radius:3px; padding:14px 20px; box-shadow:0 0 8px #eeeeee;}
        .login-form .layui-form-item {position:relative;}
        .login-form .layui-form-item label {position:absolute; left:1px; top:1px; width:38px; line-height:36px; text-align:center; color:#d2d2d2;}
        .login-form .layui-form-item input {padding-left:36px;}
        .error-msg {color: red; text-align: center; margin-bottom: 10px;}
    </style>
</head>
<body>
<div class="layui-container">
    <div class="admin-login-background">
        <div class="layui-form login-form">
            <form class="layui-form" id="resetPasswordForm">
                <div class="layui-form-item logo-title">
                    <h1>重置密码</h1>
                </div>
                <div class="layui-form-item" id="emailSection">
                    <label class="layui-icon layui-icon-email" for="email"></label>
                    <input type="email" id="email" name="email" required lay-verify="required" placeholder="请输入您的邮箱" autocomplete="off" class="layui-input">
                    <button type="button" class="layui-btn layui-btn-primary" id="sendCodeButton">发送验证码</button>
                </div>
                <div class="layui-form-item" id="codeSection">
                    <label class="layui-icon layui-icon-vercode" for="code"></label>
                    <input type="text" id="code" name="code" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item" id="passwordSection">
                    <label class="layui-icon layui-icon-password" for="newPassword"></label>
                    <input type="password" id="newPassword" name="newPassword" required lay-verify="required" placeholder="新密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item" id="confirmPasswordSection">
                    <label class="layui-icon layui-icon-password" for="confirmPassword"></label>
                    <input type="password" id="confirmPassword" name="confirmPassword" required lay-verify="required|confirmPass" placeholder="确认新密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <button id="btn-reset" class="layui-btn layui-btn layui-btn-normal layui-btn-fluid" lay-submit="" lay-filter="resetPassword">重置密码</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script th:src="@{/layui/layui.all.js}" charset="utf-8"></script>
<script th:src="@{/lib/jq-module/jquery.particleground.min.js}" charset="utf-8"></script>
<script th:inline="javascript">
    $(document).ready(function () {
        layui.use(['form', 'layer'], function () {
            var form = layui.form,
                layer = layui.layer;

            form.verify({
                confirmPass: function(value, item) {
                    if (value !== $('input[name="newPassword"]').val()) {
                        return '两次密码输入不一致';
                    }
                }
            });

            // 发送验证码操作
            $('#sendCodeButton').click(function () {
                var email = $('input[name="email"]').val();
                $.get("/email/code", { email: email }, function (result) {
                    layer.msg('验证码已发送，请检查您的邮箱。', {icon: 1, anim: 1});
                    startCountdown(120); // 开始倒计时
                }).fail(function() {
                    layer.msg('发送验证码失败，请重试。', {icon: 2, anim: 6});
                });
            });

            // 倒计时函数
            function startCountdown(seconds) {
                var remaining = seconds;
                $('#sendCodeButton').prop('disabled', true);
                $('#sendCodeButton').text(remaining + '秒后重新发送');

                var interval = setInterval(function () {
                    remaining--;
                    if (remaining > 0) {
                        $('#sendCodeButton').text(remaining + '秒后重新发送');
                    } else {
                        clearInterval(interval);
                        $('#sendCodeButton').prop('disabled', false);
                        $('#sendCodeButton').text('发送验证码');
                    }
                }, 1000);
            }

            // 重置密码操作
            form.on('submit(resetPassword)', function (data) {
                var formData = data.field;
                $.post("/resetPassword", formData, function (result) {
                    if (result.code === "1") {
                        layer.msg('密码重置成功，请重新登录。', {icon: 1, anim: 1});
                        window.location.href = "/login"; // 重置成功后跳转到登录页面
                    } else {
                        layer.msg('密码重置失败，请重试。', {icon: 2, anim: 6});
                    }
                }).fail(function() {
                    layer.msg('密码重置失败，请重试。', {icon: 2, anim: 6});
                });
                return false;
            });

            // 粒子线条背景
            $('.layui-container').particleground({
                dotColor:'#7ec7fd',
                lineColor:'#7ec7fd'
            });
        });
    });
</script>
</body>
</html>
